package com.zslibrary.Protocol;

import android.util.Log;

import com.zslibrary.BusMaster.HandleError;
import com.zslibrary.Enum.CommunicationError;
import com.zslibrary.Enum.NFCMessageId;
import com.zslibrary.Enum.Protocol;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Structure.CardStruct;
import com.zslibrary.Structure.ErrorStruct;
import com.zslibrary.Structure.GeneralRequestStruct;
import com.zslibrary.Structure.MessageStruct;
import com.zslibrary.Structure.ReplyStruct;
import com.zslibrary.Tools.FrameBuilder;
import com.zslibrary.Tools.HexBytesUtil;
import com.zslibrary.Tools.ZSLog;

public class Poll {

    public static @StateEventDefine.StateEvent int masterPoll(Pipe pipe){
        int result= StateEventDefine.EVT_NONE;
        GeneralRequestStruct request=new GeneralRequestStruct();
        request.requstId= Protocol.MSG_POLL;
        request.dataLength=0;
        ReplyStruct reply= pipe.pollPipe(request);
        if((reply!=null)&&(reply.returnCode== CommunicationError.E_SUCCESS)){
            if(reply.messages.length>0){
                for(MessageStruct message:reply.messages){
                    switch(message.messageId){
                        case Protocol.MSG_SET_COMP_ERR:{
                            CommonMessage.processSetError(message);
                        }break;

                        case Protocol.MSG_CLEAR_COMP_ERR:{
                            CommonMessage.processClrError(message);
                        }break;

                        case Protocol.MSG_BOARD_RESET:{ /*返回复位状态*/
//                            CommonMessage.processReset();
                            result=StateEventDefine.EVT_RESET;
                        }break;

                        case Protocol.MSG_BOARD_STATUS:{
                            //ZSLog.d("report status:"+HexBytesUtil.Bytes2Int(message.messageData));
                        }break;

                        default:{
                            //ZSLog.d("unknown poll code:"+message.messageId);
                        }break;
                    }
                }
            }
            return result;
        }
        return StateEventDefine.EVT_DISCONNECT;
    }

    private static NFCPollHandle mPollHandle;
    public static @StateEventDefine.StateEvent int NFCPoll(Pipe pipe){
        int result= StateEventDefine.EVT_NONE;
        GeneralRequestStruct request=new GeneralRequestStruct();
        request.requstId= Protocol.MSG_POLL;
        request.dataLength=0;
        ReplyStruct reply= pipe.pollPipe(request);
        if((reply!=null)&&(reply.returnCode== CommunicationError.E_SUCCESS)) {
            if (reply.messages.length > 0) {
                for (MessageStruct message : reply.messages) {
                    mPollHandle.handler(message);
                }
            }
            return result;
        }
        return StateEventDefine.EVT_DISCONNECT;
    }

    public interface NFCPollHandle{
        void handler(MessageStruct message);
    }

    public static void setPollHandle(NFCPollHandle handle){
        mPollHandle=handle;
    }
}
